// pages/classify/index/index.js
import shopCar from '../../../api/shopCar';
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbar: {},
    carNum:0,
    isPhone:app.globalData.isPhone,
    isIphoneX:app.globalData.isIphoneX,
    headerHeight:app.globalData.headerHeight,
    isEdit:false,
    pageNo:1,
    pageSize:30,
    shopCartList:[],
    isMore:false,
    allMoney:0,//总金额
    isAllSelect:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 页面加载
    var that = this
    if(app.globalData.isIphoneX){
      this.setData({
        isIphoneX:app.globalData.isIphoneX
      })
    }else{
      setTimeout(()=>{
        that.setData({
          isIphoneX:app.globalData.isIphoneX
        })
      },100)
    }
    app.editTabBar()
   
    if(app.globalData.isPhone){
      this.getShopCart()
    }
    

  },
  setEdit(e){
    console.log(e)
    this.setData({
      isEdit:e.currentTarget.dataset.edit==1?true:false
    })
  },
  //获取购物车列表
  getShopCart(){
    shopCar.apishopCartList().then(res=>{
      var shopCartList=res.data.map(item=>{
        item.isSelect=false
        // item.img=item.img.split(',')[0]
        return item
      })
      this.setData({
        shopCartList:shopCartList,
      })
    })
  },
  getSelectAll(){
    var isAllSelect=this.data.isAllSelect
    var allMoney=0
    var shopCartList=this.data.shopCartList.map(res=>{
      res.isSelect=!isAllSelect?true:false
      if(!isAllSelect){
        allMoney+=Number(res.price)*Number(res.num)
      }
      return res
    })
    this.setData({
      shopCartList:shopCartList,
      isAllSelect:!isAllSelect,
      allMoney:allMoney.toFixed(2)
    })
  },
  deletCar(){
  
    var ids=[]
    this.data.shopCartList.forEach(v=>{
      console.log(v)
      if(v.isSelect){
        ids.push(v.carId)
      }
    })
    if(ids.length==0){
      wx.showToast({
        title:'请先选择需要删除的商品',
          icon: 'none',
          duration:3000,
        }) 
      return false
    }
    shopCar.apishopCartDelete({ids:ids.join(',')}).then(res=>{
      this.setData({
        pageNo:1
      })
      this.getShopCart()
      this.getShopCarNum()
    })
  },
  less(e){
    var row=e.currentTarget.dataset.row
    var index=e.currentTarget.dataset.index
    var opt={
      id:row.carId,
      productId:row.productId,
      skuId:row.skuId,
      type:row.type,
      num:row.num-1>0?row.num-1:0
    }
    for (const key in opt) {
      if(!opt[key]){
        delete opt[key]
      }
    }
    if(opt.num>0){
      shopCar.apishopCartUpdate(opt).then(res=>{
        var allMoney=0
        var shopCartList=this.data.shopCartList.map(res=>{
          if(res.carId==row.carId){
            res.num=opt.num
          }
          if(res.isSelect){
            allMoney+=Number(res.price)*Number(res.num)
          }
          return res
        })
        this.setData({
          allMoney:allMoney,
          shopCartList:shopCartList
        })
        this.getShopCarNum()
      })
    }else{
      shopCar.apishopCartDelete({ids:row.carId}).then(res=>{
        var allMoney=0
        var shopCartList=this.data.shopCartList
        shopCartList.splice(index,1)
        shopCartList=shopCartList.map(res=>{
          if(res.isSelect){
            allMoney+=Number(res.price)*Number(res.num)
          }
          return res
        })
        this.setData({
          allMoney:allMoney,
          shopCartList:shopCartList
        })
        this.getShopCarNum()
      })
    }
    
  },
  add(e){
    var row=e.currentTarget.dataset.row
    var opt={
      id:row.carId,
      productId:row.productId,
      skuId:row.skuId,
      type:row.type,
      num:row.num+1
    }
    for (const key in opt) {

      if(!opt[key]){
        delete opt[key]
      }
    }
    shopCar.apishopCartUpdate(opt).then(res=>{
     
      var allMoney=0
      var shopCartList=this.data.shopCartList.map(res=>{
        if(res.carId==row.carId){
          res.num=opt.num
        }
        if(res.isSelect){
          allMoney+=Number(res.price)*Number(res.num)
        }
        return res
      })
      this.setData({
        allMoney:allMoney,
        shopCartList:shopCartList
      })
      this.getShopCarNum()
    })
    
  },
  orderConfirm(){
    var data=[]
    this.data.shopCartList.forEach(res=>{
      if(res.isSelect){
        data.push(res)
      }
    })
    if(data.length==0){
      wx.showToast({
        title: '请先选择购买的商品',
        icon:'none'
      })
      return false
    }
    wx.setStorageSync('shopCartList', data)
    wx.navigateTo({
      url: '/pages/home/orderConfirm/orderConfirm',
    })
  },
  getSelect(e){
    var carId= e.currentTarget.dataset.id
    var allMoney=0
    var shopCartList=this.data.shopCartList.map(res=>{
      console.log(res.carId,carId)
      if(res.carId==carId){        
        res.isSelect=!res.isSelect
      }
      if(res.isSelect){
        allMoney+=Number(res.price)*Number(res.num)
      }
      return res
    })
    this.setData({
      allMoney:allMoney,
      shopCartList:shopCartList,
      isAllSelect:this.areAllValuesEmpty(shopCartList),
    })
  },
  areAllValuesEmpty(arr) {
    return arr.every(item => item.isSelect);
},
getShopCarNum(){
  shopCar.apishopCarNum().then(res=>{
    this.setData({
      carNum:res.data?res.data.num:0,
    })
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(app.globalData.isPhone){
      this.getShopCarNum()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var userId=wx.getStorageSync('userInfo')?wx.getStorageSync('userInfo').userId:''
    return {
      title: '火食集', // 分享标题
      path: `/pages/home/index/index?referrerId=${userId}`, // 分享路径，必须是已经存在的页面路径
      // imageUrl: 'https://example.com/share.jpg', // 分享图标
      success: function (res) {
        // 转发成功
        if (res.errMsg === 'shareAppMessage:ok') {
          console.log('分享成功');
        }
      },
      fail: function (res) {
        // 转发失败
        console.log('分享失败', res);
      }
    }
  }
})